

//
//  zhuCeViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "zhuCeViewController.h"
#import "zhuCeView.h"
#import "DataBaseSingleton.h"
#import "user.h"
@interface zhuCeViewController ()
@property(nonatomic,retain)zhuCeView *zhuce;
@end

@implementation zhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    
    self.zhuce = [[zhuCeView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.zhuce];
    [_zhuce release];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)]autorelease];
    
    
    
}

//注册按钮
-(void)rightButton:(UIBarButtonItem *)btn
{
    //开建数据库
    [[DataBaseSingleton shareDataBase]openDB];
    NSString *login = self.zhuce.loginText.text;
    NSString *password = self.zhuce.passWordText.text;
    
    
    if ( login.length >0 &&  password.length>0) {
        
        
        NSMutableArray *allArray = [[DataBaseSingleton shareDataBase]selectUser];
        NSMutableArray *allNameArray = [NSMutableArray array];
        for (user *myuser in allArray) {
            NSString *name = myuser.userName;
            [allNameArray addObject:name];
        }
        if ([allNameArray containsObject:login]) {
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"用户已经存在" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
            [actionsheet showInView:self.view.window];
            [actionsheet release];
            
        }
        
        
        
        
        else{
            NSUserDefaults *uerDefault1 = [NSUserDefaults standardUserDefaults];
            [uerDefault1 setObject:@"1" forKey:@"isLogin"];
            
            NSUserDefaults *uerDefault = [NSUserDefaults standardUserDefaults];
            
            [uerDefault setObject:login forKey:@"name"];
            
            [uerDefault synchronize];
            
            //创建用户表
            
            [[DataBaseSingleton shareDataBase]createUserTable];
            
            
            //插入数据
            
            user *myuser = [[user alloc]init];
            myuser.userName = login;
            myuser.password = password;
            [[DataBaseSingleton shareDataBase]insertStudent:myuser];
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            }];
            
            [alertC addAction:action];
            
            
            
        }
    }
    
    
    
    else
    {
        
        UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"姓名或密码不能为空" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
        [actionsheet showInView:self.view.window];
        [actionsheet release];
        return;
        
        
    }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
